﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace FuncWorks.Xna.UI
{
	public static class XnaConsole
	{
		public static int Spacer { get; set; }
		public static SpriteFont Font { get; set; }
		public static Rectangle? OutputArea { get; set; }
		static List<string> buffer = new List<string>();


		public static void Clear()
		{
			buffer = new List<string>();
		}

		public static void WriteLine(object message)
		{
			if (message == null) { WriteLine(""); }
			else { WriteLine(message.ToString()); }
		}

		public static void WriteLine(string format, params object[] args)
		{
			WriteLine(String.Format(format, args));

		}

		public static void WriteLine(string message)
		{
			buffer.Add(message);
		}

		public static void Draw(GraphicsDeviceManager graphics, SpriteBatch batch)
		{
			if (OutputArea == null) { OutputArea = graphics.GraphicsDevice.Viewport.TitleSafeArea; }
			Rectangle rect = (Rectangle)OutputArea;

			int current = rect.Bottom;

			for (int i = buffer.Count - 1; i >= 0; i--)
			{
				current -= (int)Math.Ceiling(Font.MeasureString(buffer[i]).Y);
				batch.DrawString(Font, buffer[i], new Vector2(0, current), Color.White);
				current -= Spacer;
				if (current < rect.Top) { break; }
			}
		}
	}
}
